<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Controller;

/**
 * 前台首页控制器
 * 主要获取首页聚合数据
 */
class IndexController extends HomeController
{

    //系统首页
    public function index()
    {
        $bb = M('Category')->where(['pid'=>0])->limit(3)->getField('title', true);
        
       $good=$this->xxx();
     
        $category = D('Category')->getTree(0,'id,title,pid'); 
    
        $this->assign('bb',$bb);
        $lists= D('Document')->lists(null);
        $this->assign('category', $category); //栏目
        $this->assign('lists', $lists); //列表
         $this->assign('good', $good);
        $this->assign('page', D('Document')->page); //分页
              // var_dump(session('islogin'));
        $this->display();
    }

public function xxx($pid=0)
    {
        // 查询所有顶级分类
        $arr = M('Category')->where('pid='.$pid)->limit(3)->getField('id', true);
      
        // 随机取三个顶级分类
        // $arr    = array_rand(array_flip($dingji), 3);
        $tmp    = [];
         
        // 把取到的三个顶级分类下面的子分类全部猎取出来
        foreach ($arr as $v) {
            $tmp[] = $this->digui($v, 'id,title,pid');
        }

        // 把猎取到的子分类的id 分类放到数组中
        $whereArr = [];
        foreach ($tmp as $k => $v) {
            foreach ($v as $vv) {
                $whereArr[$k][] = $vv['id'];
            }
        }
        
        // 根据子分类的id 查询 商品 cate id数组当中的商品
        foreach ($whereArr as $k => $v) {
        	
            $where['pid'] = ['in', $v];
            $good[]       = M('good')->where($where)->limit(8)->select();
        }
        return $good;
    
    }

    public function digui($pid = 0, $field = true, $arr = [], $lv = 0)
    {
        $cate = M('Category')->where('pid=' . $pid)->field($field)->select();
        if ($cate) {
            $lv++;
            foreach ($cate as &$v) {
                $v['lv'] = $lv;
                $arr[]   = $v;
                $arr     = $this->digui($v['id'], $field, $arr, $lv);
            }
        }
        return $arr;
    }



    public function login()
    {
        if (IS_GET) { 
            $this->display();
        } else {

        }

    }

    public function check_verify($code, $id = '')
    {
        $verify = new \Think\Verify();
        return $verify->check($code, $id);
    }

    public function yzm()
    {
        $config = array(
            'fontSize' => 30, // 验证码字体大小
            'length'   => 4, // 验证码位数
            'useNoise' => true, // 关闭验证码杂点
        );
        $yzm = new \Think\Verify($config);
        $yzm->entry();
    }

    public function register()
    {
        if (IS_GET) {
            $this->display();
        } else {
            if (!$this->check_verify(I('post.yzm'))) {
                $this->error('验证码错误');
            }
            $form = D('User');
            $data = $form->create();
            if ($data) {
                if ($form->add($data)) {
                    $this->success('注册成功', U('login'));
                } else {
                    $this->error('注册失败');
                }
            } else {
                $this->error($form->getError());
            }
        }

    }

    public function denglu()
    {
        if (IS_GET) {
            $this->display();
        } else {
            $form = D('User');
            $res  = $form->where(['email' => I('post.email'), 'pwd' => I('post.pwd')])->find();

            if ($res) {
                session('islogin', 1);
                unset($res['pwd']);
                session('res', $res);
                $this->success('欢迎回来', U('index'));
            } else {
                $this->error('登陆失败');
            }

        }
    }
    public function zhuxiao(){
    	
    	session(null);
    	$this->display('index');

    }



    public function introduction(){

           $res=M('good')->where(['id'=>I('get.id')])->find();
           $cate=get_parent_category($res['pid']);
        $attrclass=M('attrclass')->where(['gid'=>I('get.id')])->select();
          $attrval=M('attrval')->where(['gid'=>I('get.id')])->select();
      
    
      foreach ($attrclass as  &$v) {
          foreach ($attrval as  $vv) {
           if ($v['id']==$vv['classid']) {
            $v['sb'][]=$vv;
           }
           
          }
      }

  
           $this->assign('res',$res);
         $this->assign('cate',$cate);
         $this->assign('attrclass',$attrclass);
         
        $this->display();
     
    }


    public function ajaxPrice(){
        $where['gid']=I('post.gid');
        $where['attrvid']=implode(',', I('post.attrvid'));
        $res=M('attrprice')->where($where)->find();
        
        $this->ajaxReturn($res);
    }



}
